/* D:\ICADDEV\PRJ\ICAD\PREFERENCES.CPP
 * Copyright (C) 1997-1999 DIMINE Co.LTD. All rights reserved.
 * 
 * $Revision: 1.1.1.1 $ $Date: 2010/03/03 02:57:12 $ 
 * 
 * Abstract
 * 
 * Common utilities for managing user preferences.
 * 
 */ 

#include "stdafx.h"
//#include "Icad.h"
#include "dmlib.h"

#include "dm_const_define.h"
//#include "IcadCursor.h"

#include "Preferences.h"
#include "resource.h"


char	PreferenceKey::m_applicationRoot[MAX_PATH]="";




PreferenceKey::PreferenceKey( HKEY tree, const CString& subkey, bool create)
	{
/*
	CRegKey	root;

		// initialize static string for application root in registry
//	if ( ! m_bStart)
		{
//		m_bStart=TRUE;
	
		CString		temp;
		const int	length = 100;
		LPTSTR		buffer = temp.GetBuffer( length);

								// don't rely on CString::LoadString in static constructor
		int loaded = ::LoadString( ::GetModuleHandle( NULL), IDS_REGISTRY_ROOT, buffer, length);

								// last resort if string resource fails
		if ( !loaded )	
		{
			//Modified Cybage VSB 13/04/2001 DD/MM/YYYY [
			//Reason : Mail from Surya Sarda dated 06/04/2001 
			//temp = TEXT( "Software\\ITC\\DIMINE");
			temp = TEXT( ResourceString( IDC_SOFTWARE_ITC_DIMINE, "Software\\ITC\\DIMINE"));
			//Modified Cybage VSB 13/04/2001 DD/MM/YYYY ]
		}

	//	m_applicationRoot = temp;
		strcpy(m_applicationRoot ,temp);


		}

	if (create)
		{
		root.Create( tree, m_applicationRoot);
		VERIFY( Create( root, subkey) == ERROR_SUCCESS );
		}
	else
		{
		root.Open(tree, m_applicationRoot, KEY_READ | KEY_CREATE_SUB_KEY);
		Open(root, subkey);
		}
*/
	CRegKey	root;
	
	// initialize static string for application root in registry
	if ( strcmp(m_applicationRoot,"")==0 )
//	if ( m_applicationRoot.GetBuffer(100) EQ NULL )
	{
	//	m_applicationRoot.Init();
		CString		temp;
		const int	length = 100;
		LPTSTR		buffer = temp.GetBuffer( length);
		
								// don't rely on CString::LoadString in static constructor
		int loaded = ::LoadString( ::GetModuleHandle( NULL), IDS_REGISTRY_ROOT, buffer, length);
		
								// last resort if string resource fails
		if ( !loaded )	
		{
			//Modified Cybage VSB 13/04/2001 DD/MM/YYYY [
			//Reason : Mail from Surya Sarda dated 06/04/2001 
		
		//	temp = TEXT( ResourceString( IDC_SOFTWARE_ITC_DIMINE, "Software\\ITC\\DIMINE"));
			//Modified by tzh 
			temp="";
		}

		strcpy(m_applicationRoot,temp.GetBuffer(temp.GetLength()));
		
	//	m_applicationRoot = temp;
		
		
	}
	
	if (create)
	{
		root.Create( tree, m_applicationRoot);
		VERIFY( Create( root, subkey) EQ ERROR_SUCCESS );
	}
	else
	{
		root.Open(tree, m_applicationRoot, KEY_READ | KEY_CREATE_SUB_KEY);
		Open(root, subkey);
	}

	}

LONG PreferenceKey::FlushKey(HKEY tree, const CString& keyname)
	{
	CRegKey root;
	root.Open(tree, m_applicationRoot);
	root.RecurseDeleteKey(keyname);
	return Create(root, keyname);
	}

BooleanPreference::BooleanPreference( CRegKey& key, const CString name, bool defaultValue)
		: RegistryPreference( key), m_name( name), m_default( defaultValue)
	{
	DWORD	value;

	if ( m_key.QueryValue( value, m_name) == ERROR_SUCCESS )
		m_value = value ? true : false;
	else
		m_value = m_default;
	}


BooleanPreference::~BooleanPreference()
	{
	if ( m_value == m_default )
		m_key.DeleteValue( m_name);
	else
		VERIFY( m_key.SetValue( m_value, m_name) == ERROR_SUCCESS );
	}


IntPreference::IntPreference( CRegKey& key, const CString name, int defaultValue)
		: RegistryPreference( key), m_name( name), m_default( defaultValue)
	{
	DWORD	value;

	if ( m_key.QueryValue( value, m_name) == ERROR_SUCCESS )
		m_value = (int)value;
	else
		m_value = m_default;
	}


IntPreference::~IntPreference()
	{
	if ( m_value == m_default )
		m_key.DeleteValue( m_name);
	else
		VERIFY( m_key.SetValue( m_value, m_name) == ERROR_SUCCESS );
	}


static	PreferenceKey		configKey( HKEY_CURRENT_USER, "Config");
		BooleanPreference	UserPreference::crosshairPreference( configKey, "AlwaysUseCrosshair");
		IntPreference		UserPreference::crosshairSizePreference( configKey, "CursorSize", 5);
		BooleanPreference	UserPreference::GripsAttractionPreference(configKey, "EnableAttraction", true);
		IntPreference		UserPreference::GripsAttractionDistancePreference(configKey, "AttractionDistance", 3);

		BooleanPreference	UserPreference::ContinuousMotionPreference(configKey, "ContinuousMotion", false);

		// DP: Now we save and load this variable together with others.
		//IntPreference		UserPreference::OSModePreference(configKey, "OSMODE", 0);

		BooleanPreference	UserPreference::SnapMarkerPreference(configKey, "DisplaySnapMarker", true);
		IntPreference		UserPreference::SnapMarkerSizePreference(configKey, "SnapMarkerSize", 6);
		IntPreference		UserPreference::SnapMarkerThicknessPreference(configKey, "SnapMarkerThickness", 2);
		IntPreference		UserPreference::SnapMarkerColorPreference(configKey, "SnapMarkerColor", 2);
		BooleanPreference	UserPreference::SnapMarkerDrawInAllViews(configKey, "SnapMarkerDrawInAllViews", false);
		BooleanPreference	UserPreference::SnapTooltipsPreference(configKey, "DisplayTooltips", true);
		BooleanPreference	UserPreference::SnapApertureBoxPreference(configKey, "DisplaySnapApertureBox", true);;

	// must follow preference declarations for proper construction
    //IcadCursor	cursorSingleton;




